#pragma once

#include <functional>
#include <memory>
#include <string>
#include <list>
#include <unordered_set>

#include <cstring>
#include <inttypes.h>

#include "Singleton.hpp"

#include "hv/hv.h"
#include "hv/hloop.h"
#include "hv/hlog.h"

#include "utils.h"
#include "debug_backtrace.h"

#include "pb_decode.h"
#include "pb_encode.h"
#include "local_registry.pb.h"

const constexpr std::size_t LOCAL_REGISTRY_HV_LOOP_NUM_MAX = 3;
const constexpr std::size_t LOCAL_REGISTRY_HV_MSG_HEADER_SIZE = sizeof(st_local_msg_header);
const constexpr std::size_t LOCAL_REGISTRY_HV_MSG_REMOTE_HEADER_SIZE = sizeof(uint32_t) * 5; // client_id, msg_seqid, msg_type, service_id, msgdata_len

#define LOCAL_REGISTRY_SOCKET_LEN_MAX   (108)
#define LOCAL_REGISTRY_CLIENT_NAME_MAX  (32)
#define SOCKET_PATH_PREFIX              "/tmp/"
#define LOCAL_REGISTRY_SOCKET_FILE      "local_registry.ipc"
#define LOCAL_REGISTRY_SOCKET_PATH      SOCKET_PATH_PREFIX LOCAL_REGISTRY_SOCKET_FILE
#define LOCAL_REGISTRY_CTRL_SOCKET_FILE "local_registry_ctrl.ipc"
#define LOCAL_REGISTRY_CTRL_SOCKET_PATH SOCKET_PATH_PREFIX LOCAL_REGISTRY_CTRL_SOCKET_FILE

#define LOCAL_REGISTEY_SOCKET_FMT  SOCKET_PATH_PREFIX "%u-%s.ipc"       // client_pid-client_name.ipc
#define LOCAL_REGISTEY_SOCKET_FMT1 SOCKET_PATH_PREFIX "%u-listen-1.ipc" // 监听 client_id-listen-1.ipc
#define LOCAL_REGISTEY_SOCKET_FMT2 SOCKET_PATH_PREFIX "%u-%s-%u-2.ipc"  // 通知 client_src_id-client_target_id-2.ipc

#define LOCAL_REGISTRY_CLIENT_ONCE_COUNT_MAX                  (128) // client count max
#define LOCAL_REGISTRY_CLIENT_PROVIDE_SERVICES_ONCE_COUNT_MAX (128) // one client can provider max 128 services
#define LOCAL_REGISTRY_CLIENT_CONSUME_SERVICES_ONCE_COUNT_MAX (128) // one client can consumer max 128 services

#define LOCAL_REGISTRY_COMMUNICATION_TIMEOUT_MS (1500)
